package com.Logistics.pddispatch.dto;

import com.alibaba.nacos.common.utils.Md5Utils;
import com.Logistics.pddispatch.entity.CacheLine;
import com.Logistics.pddispatch.entity.CacheLineDetail;
import com.Logistics.entity.line.LineBean;
import com.Logistics.mycommon.tools.IDAlgorithm;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;

public class AnalysisRoutePlanningDTO {
    private Map<Integer, List<CacheLineDetail>> lineMap = new HashMap<>();
    private Map<String, LineBean> transportLineDtoMap = new HashMap<>();

    private RoutePlanningDTO routePlanningDTO;
    private String startAgencyId;
    private String endAgencyId;

    private Integer index = 0;

    /**
     * 在此加工可用线路信息 返回完整的数据格式
     */
    public Map<CacheLine, List<CacheLineDetail>> get() {
        Map<CacheLine, List<CacheLineDetail>> result = new HashMap<>();
        for (List<CacheLineDetail> value : lineMap.values()) {
            Collections.reverse(value);
            String cacheLineId = new IDAlgorithm(0L,0L).nextIdAsString();
            BigDecimal distance = new BigDecimal(0);
            BigDecimal cost = new BigDecimal(0);
            BigDecimal estimateTime = new BigDecimal(0);
            StringBuffer sbf = new StringBuffer();
            for (int i=0;i<value.size();i++){
                CacheLineDetail cacheLineDetail = value.get(i);
                cacheLineDetail.setSort(i+1);
                cacheLineDetail.setCache_line_id(cacheLineId);
                LineBean lineBean = transportLineDtoMap.get(cacheLineDetail.getTransport_line_id());
                distance = distance.add(lineBean.getDistance());
                cost = cost.add(lineBean.getCost());
                estimateTime = estimateTime.add(lineBean.getEstimated_time());
                sbf.append(lineBean.getStart_agency_id()).append("#")
                        .append(lineBean.getEnd_agency_id()).append("#")
                        .append(lineBean.getDistance().toString()).append("#")
                        .append(lineBean.getCost().toString()).append("#")
                        .append(lineBean.getEstimated_time().toString()).append("#");
            }
            CacheLine cacheLine = new CacheLine();
            cacheLine.setId(cacheLineId);
            cacheLine.setDistance(distance);
            cacheLine.setCost(cost);
            cacheLine.setEstimate_time(estimateTime);
            cacheLine.setVerify_key(Md5Utils.getMD5(sbf.toString().getBytes()));
            cacheLine.setIs_current(1);
            cacheLine.setTransfer_count(value.size());
            cacheLine.setStart_agency_id(this.startAgencyId);
            cacheLine.setEnd_agency_id(this.endAgencyId);
            result.put(cacheLine, value);

        }
        return result;
    }

    public AnalysisRoutePlanningDTO(RoutePlanningDTO routePlanningDTO, String startAgencyId, String endAgencyId) {
        this.routePlanningDTO = routePlanningDTO;
        this.startAgencyId = startAgencyId;
        this.endAgencyId = endAgencyId;
    }

    public AnalysisRoutePlanningDTO build() {
        build(this.routePlanningDTO);
        return this;
    }

    /**
     * 获取全部可到达线路
     * 遍历之前查找到的线路，feasibleTransportLineDtos 不为空的时候表示该条线路可以通行。
     * @param routePlanningDTO
     * @return
     */
    private Integer[] build(RoutePlanningDTO routePlanningDTO) {
        List<Integer> indexList = new ArrayList<>();
        //获取当前的转运线路信息
        List<LineBean> lineBeanList = routePlanningDTO.getNext();
        if (!CollectionUtils.isEmpty(lineBeanList)){
            for (int i=0;i<lineBeanList.size();i++){
                LineBean lineBean = lineBeanList.get(i);
                RoutePlanningDTO routePlanning = routePlanningDTO.get(lineBean);
                Integer[] result = build(routePlanning);
                if (result.length>0){
                   for (Integer integer : result) {
                       //添加线路
                       CacheLineDetail cacheLineDetail = new CacheLineDetail();
                       cacheLineDetail.setCache_line_id(lineBean.getId());
                       cacheLineDetail.setStart_agency_id(lineBean.getStart_agency_id());
                       cacheLineDetail.setEnd_agency_id(lineBean.getEnd_agency_id());
                       lineMap.get(integer).add(cacheLineDetail);
                       indexList.add(integer);
                       transportLineDtoMap.put(lineBean.getId(),lineBean);
                   }
                }
            }
        }

        //获取当前所有直达线路
        List<LineBean> beanList = routePlanningDTO.getFeasible();
        if (!CollectionUtils.isEmpty(beanList)){
            for (int i=0;i<beanList.size();i++){
                LineBean lineBean = beanList.get(i);
                CacheLineDetail cacheLineDetail = new CacheLineDetail();
                cacheLineDetail.setCache_line_id(lineBean.getId());
                cacheLineDetail.setStart_agency_id(lineBean.getStart_agency_id());
                cacheLineDetail.setEnd_agency_id(lineBean.getEnd_agency_id());
                List<CacheLineDetail> cacheLineDetails = new ArrayList<>();
                cacheLineDetails.add(cacheLineDetail);
                lineMap.put(++index,cacheLineDetails);
                indexList.add(index);
                transportLineDtoMap.put(lineBean.getId(),lineBean);
            }
        }

        if (indexList.size()>0){
            return indexList.toArray(new Integer[indexList.size()]);
        }else {
            return new Integer[0];
        }
    }

}
